//
//  WCTextFile.h
//  WabbitCode
//
//  Created by William Towe on 1/23/09.
//  Copyright 2009 Revolution Software. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import "WCFile.h"


extern NSString *kWCTFFileChangeCountDidChangeNotification;
extern NSString *kWCTFFileNumberOfErrorsDidChangeNotification;
extern NSString *kWCTFFileNumberOfWarningsDidChangeNotification;
extern NSString *kWCTFFileNumberOfBreakpointsDidChangeNotification;
extern NSString *kWCTFFileStatusOfBreakpointsDidChangeNotification;

@class WCBreakpoint,WCTextView,WCFileError,WCFileWarning;

@interface WCTextFile : WCFile <NSCoding, NSCopying> {
	// file attributes
	NSDictionary *_fileAttributes;
	// file encoding to use when opening and saving the file
	NSStringEncoding _fileEncoding;
	// holds the contents of each text file, shared between each text view that references this text file
	NSTextStorage *_fileTextStorage;
	// character index of the start of each line in our string
	NSMutableArray *_lineStartIndexes;
	// undo manager for this text file
	NSUndoManager *_fileUndoManager;
	// change count for the undo manager
	NSInteger _fileChangeCount;
	// text view for find and replace in project
	NSScrollView *_scrollViewWithTextViewForReplace;
	
	// contains the label names as NSString objects
	NSMutableSet *_fileLabels;
	// contains the equate names as NSString objects
	NSMutableSet *_fileEquates;
	// contains the macro names as NSString objects
	NSMutableSet *_fileMacros;
	// contains the define names as NSString objects
	NSMutableSet *_fileDefines;
	// contains the WCSymbol objects representing labels in the order they are encountered
	NSMutableArray *_fileLabelsArray;
	// contains the WCSymbol objects representing equates in the order they are encountered
	NSMutableArray *_fileEquatesArray;
	// contains the WCSymbol objects from the previous two arrays in order
	NSMutableArray *_fileSymbolsArray;
	
	// breakpoint markers for this file
	NSMutableDictionary *_fileBreakpointMarkers;
	// line indexes for errors
	NSMutableDictionary *_fileErrorLineNumbers;
	// line indexes for warnings
	NSMutableDictionary *_fileWarningLineNumbers;
	
	// separate editor window frame
	NSString *_separateEditorWindowFrameString;
	// saved selected range
	NSRange _fileSelectedRange;
	
	struct __WCTextFileFlags {
		unsigned int rebuildFileSymbols:1; // we need to rebuild our symbols 
		unsigned int reserved:31;
		
	} _WCTextFileFlags;
}
@property (readwrite, retain) NSDictionary *fileAttributes;
@property (readwrite, assign) NSStringEncoding fileEncoding;
@property (readonly) NSUndoManager *fileUndoManager;
@property (readwrite, assign) NSInteger fileChangeCount;
@property (readonly) BOOL isEdited;
@property (readonly) NSTextStorage *fileTextStorage;
@property (readonly) NSString *fileContents;
@property (readonly) NSUInteger totalErrorsAndWarnings;
@property (readonly) NSSet *fileLabels;
@property (readonly) NSSet *fileEquates;
@property (readonly) NSSet *fileMacros;
@property (readonly) NSSet *fileDefines;
@property (readonly) NSArray *fileLabelsArray;
@property (readonly) NSArray *fileEquatesArray;
@property (readonly) NSArray *fileSymbolsArray;
@property (readwrite, assign) BOOL rebuildFileSymbols;
@property (readonly) NSMutableArray *lineStartIndexes;
@property (readonly) NSUInteger numberOfFileSymbols;
@property (readwrite, assign) NSRange fileSelectedRange;
@property (readonly) WCTextView *textViewForReplace;
@property (readwrite, copy) NSString *separateEditorWindowFrameString;

- (NSUInteger)lineNumberForCharacterIndex:(NSUInteger)index;
- (NSUInteger)lineStartIndexForLineNumber:(NSUInteger)lineNumber;

- (WCBreakpoint *)addFileBreakpointAtLineNumber:(NSUInteger)ln;
- (void)addFileBreakpoint:(WCBreakpoint *)bp atLineNumber:(NSUInteger)ln;
- (void)removeFileBreakpointAtLineNumber:(NSUInteger)ln;
- (WCBreakpoint *)fileBreakpointAtLineNumber:(NSUInteger)ln;
- (void)toggleBreakpointAtLineNumber:(NSUInteger)ln;
- (void)toggleBreakpoint:(WCBreakpoint *)bp;
- (NSUInteger)numberOfFileBreakpoints;
- (void)enableAllFileBreakpoints;
- (void)disableAllFileBreakpoints;
- (void)removeAllFileBreakpoints;

- (void)addFileError:(WCFileError *)fileError atLineNumber:(NSUInteger)ln;
- (void)removeFileErrorAtLineNumber:(NSUInteger)ln;
- (void)removeAllFileErrors;
- (BOOL)containsFileErrorAtLineNumber:(NSUInteger)ln;
- (WCFileError *)fileErrorAtLineNumber:(NSUInteger)ln;

- (void)addFileWarning:(WCFileWarning *)fileWarning atLineNumber:(NSUInteger)ln;
- (void)removeFileWarningAtLineNumber:(NSUInteger)ln;
- (void)removeAllFileWarnings;
- (BOOL)containsFileWarningAtLineNumber:(NSUInteger)ln;
- (WCFileWarning *)fileWarningAtLineNumber:(NSUInteger)ln;

- (void)saveTextFile;
- (void)saveTextFileAsAndCloseIfCancelled:(BOOL)closeIfCancelled;
- (void)resetTextFile;
- (void)revertTextFile;
- (void)validateTextFile;

+ (id)textFileWithFilePath:(NSString *)filePath fileEncoding:(NSStringEncoding)encoding inProject:(WCProject *)project;

- (id)initWithFilePath:(NSString *)filePath fileEncoding:(NSStringEncoding)encoding inProject:(WCProject *)project;

@end
